package com.sumer.juc;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

/**
 * map线程非安全测试
 */
public class MapThreadTest {

    public static void main(String[] args) {

//        Map<String, String> map = new HashMap<>(); //不安全
//        Map<String, String> map = Collections.synchronizedMap(new HashMap<>()); // 安全
        Map<String, String> map =  new ConcurrentHashMap<>();// 安全

        for (int i = 0; i < 10000; i++) {
            new Thread(()->{
                map.put(UUID.randomUUID().toString(),new Data().toString());
            }).start();
        }
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(map.size());
    }
}
